//验证码登录 - 输入验证码
let api = require("../../../server/api.js")
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    send: true, // 未发送前
    alreadySend: false, // 用于判断是否已发送验证码
    phone: 0,
    verifyCode: 0,
    inviteCode: '',
    second: 60, // 60秒倒计时时间
    msgId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function(options) {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  phone:function(e){
    this.setData({
      phone:e.detail.value
    })
  },

  // 获取验证码倒计时
  getVerifyCode: function() {
    if (!this.data.phone) {
      wx.showModal({
        title: '提示',
        content: '请输入手机号',
      })
      return
    }
    console.log('倒计时开始')
    api.getVerifyCode(this.data.phone).then(res => {
      console.log(res)
      if (res) {
        this.setData({
          msgId: res,
          alreadySend: true,
          send: false
        })
        this.timer()
      }
    }).catch(msg => {
      console.log(msg)
    })
  },
   // 定时器
  timer: function() {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(() => {
        this.setData({
          second: this.data.second - 1
        })
        if (this.data.second <= 0) {
          this.setData({
            second: 60,
            alreadySend: false,
            send: true
          })
          resolve(setTimer)
        }
      }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  // 电话登录
  loginByMobile: function (e) {
    let phone = this.data.phone;
    let verifyCode = e.detail.value.verifyCode;
    let inviteCode = e.detail.value.inviteCode;
    let msgId = this.data.msgId
    if (!msgId) {
      wx.showModal({
        title: '提示',
        content: '请获取短信验证码',
      })
      return;
    }

    if (!verifyCode) {
      wx.showModal({
        title: '提示',
        content: '请输入短信验证码',
      })
      return;
    }
    if (inviteCode != "" && inviteCode.length != 7) {
      app.showToast("邀请码必须是七位数!")
      return;
    }

  },
})